<?php

function getFriendsForUser($user){
      // Create our Application instance (replace this with your appId and secret).
      $facebook = new Facebook(array(
	'appId'  => '176213615761250',
	'secret' => '375110a154f2a4f5b2f6e4f8f093f7be',
      ));

      // Get User ID
      $user = $facebook->getUser();


      if ($user){
      //echo "$user <br><br>";
	try {
	      $friends = $facebook->api('/me/friends');
      // 	print_r($friends);
	      
	      $i = 0;
	      foreach ($friends["data"] as $friend){
		    $friendsID[$i] = $friend["id"];
		    $i++;
	      }
      // 	print_r($friendsID);
      // echo "<br><br>";
	      $con = mysql_connect ("localhost:3306", "root", "fli2411");
	      
	      if (!$con) {
		error(DB_ERR);
	      }
	      
	      mysql_select_db("brain_browser", $con);
	      $result = mysql_query("SELECT user FROM user_link", $con);
	      
	      if (!$result) {
		error(DB_ERR);
	      }
	      else {
		      $i = 0;
		      while ($fetchedArray[$i] = mysql_fetch_array($result, MYSQL_NUM) )
			      $i++;
			
		      $i = 0;
		      foreach ($fetchedArray as $ID){
      // 			echo "$ID[0] <br>";
			      $userIDs[$i] = $ID[0];
			      $i++;
		      }
		      
		      $userIDs = array_unique($userIDs);
      // 		print_r($userIDs);

		      $appFriends = array_intersect($friendsID, $userIDs);
      // 		echo "<br><br>App Friends<br><br>";
      // 		print_r($appFriends);
		      
		      return $appFriends;
	      }

	  } catch (FacebookApiException $e) {
		error_log($e);
		$user = null;
      // 	  echo "An error occured! Try again!<br><br> $e";
		return -1;
	}
      }
      else {
	return -1;
      }
}

?>